import math
import config
import gtk.gdk

class Drawing:
    
    def __init__(self, drawArea):
        
        self.drawArea = drawArea   
        self.gc = self.drawArea.window.new_gc()        
        self.w, self.h = self.drawArea.window.get_size()
        self.buffer = gtk.gdk.Pixmap(self.drawArea.window, self.w, self.h)   
        self.cr = self.buffer.cairo_create()
    
    
    
    def startDrawing(self, bg=None):   
        
        self.cr.identity_matrix()
        wi, hi = self.w - 2 * config.margin, self.h - 2 * config.margin

        self.paint((1, 1, 1, 1))
        self.cr.set_source_rgba(*config.editorBgColor)
        self.cr.rectangle(0, 0, self.w, self.h)
        self.cr.fill()
        
        
        self.cr.scale(wi, hi)
        self.cr.translate(config.margin / float(wi), config.margin / float(hi)) 
     
        self.cr.set_source_rgba(*config.editorFgColor)
        self.cr.rectangle(0, 0, 1, 1)
        self.cr.fill()
        
        if bg is not None:
            self.drawPixbuf(bg[0])
            c = config.editorFgColor
            self.cr.set_source_rgba(c[0], c[1], c[2], bg[1])
        else:
            self.cr.set_source_rgba(*config.editorFgColor)
        self.cr.rectangle(0, 0, 1, 1)
        self.cr.fill()

    def drawPixbuf(self, pixbuf):
        marg = config.margin
        pw, ph = pixbuf.get_width(), pixbuf.get_height()
        cw = self.w - 2 * marg
        ch = self.h - 2 * marg
        w = min(cw, pw)
        h = min(ch, ph)
        dstx = marg
        dsty = marg
        dstx += (cw - w) // 2
        dsty += (ch - h) // 2
        self.buffer.draw_pixbuf(self.gc, pixbuf, 0, 0, dstx, dsty, w, h)
        
    def paint(self, color):
        self.cr.set_source_rgba(*color)
        self.cr.paint()
        
    def drawCircle(self, x, y, radius, color):
        self.cr.set_source_rgba(*color)
        self.cr.arc(x, 1.0 - y, radius, 0, 2 * math.pi)
        self.cr.fill()            

    def drawLineA(self, x1, y1, x2, y2, width, color):
        
        self.cr.set_source_rgba(*color)
        self.cr.set_line_width(width)

        self.cr.move_to(x1, 1.0 - y1)
        self.cr.line_to(x2, 1.0 - y2)
        self.cr.stroke()       

    def drawLine(self, x1, y1, x2, y2, width, color):

        self.cr.set_source_rgba(*color)
        self.cr.set_line_width(width)

        self.cr.move_to(x1, 1.0 - y1)
        self.cr.line_to(x2, 1.0 - y2)
        self.cr.stroke()
    
    def drawArrow(self, x1, y1, x2, y2, width, color):
        
        self.drawLine(x1, y1, x2, y2, width, color)
      
        ln = config.arrowLen
        wd = config.arrowWidth
        
        mat = self.cr.get_matrix()
        self.cr.translate(x2, 1-y2)
        self.cr.rotate(-math.atan2(y2 - y1, x2 - x1))

        self.cr.move_to(0, 0)
        self.cr.line_to(-ln, -wd / 2)
        self.cr.line_to(-ln, wd / 2)
        self.cr.close_path()
        self.cr.fill()
        self.cr.set_matrix(mat)
    
    def drawRect(self, x1, y1, x2, y2, width, color):
        x1, x2 = min(x1, x2), max(x1, x2)
        y1, y2 = min(y1, y2), max(y1, y2)
        self.cr.set_source_rgba(*color)
        self.cr.rectangle(x1, 1.0 - y1, x2 - x1, y1 - y2)
        self.cr.fill()

    def endDrawing(self):
        
        self.drawArea.window.draw_drawable(self.gc, self.buffer, 0, 0, 0, 0, -1, -1)    
        
